/**
 * 
 */
package com.gr.staffpm.notes;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.ComponentTag;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.BookmarkablePageLink;
import org.apache.wicket.markup.html.panel.Panel;
import org.joda.time.Days;
import org.joda.time.Hours;
import org.joda.time.Instant;
import org.joda.time.Minutes;
import org.joda.time.Period;
import org.joda.time.PeriodType;
import org.joda.time.Seconds;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.PeriodFormatter;
import org.joda.time.format.PeriodFormatterBuilder;

import com.gr.staffpm.notes.activity.ActivityMessage;
import com.gr.staffpm.pages.tasks.ViewTask;


/**
 * @author Graham Rhodes 20 Feb 2011 16:33:26
 */
public class NotificationPanel extends Panel {

    private static final long serialVersionUID = 1L;
    private final Label activityContent;
    private final BookmarkablePageLink<Void> contentLink;
    private final Label timestampLabel;

    public NotificationPanel(String id, final ActivityMessage activity) {
        super(id);

        contentLink = new BookmarkablePageLink<Void>("contentLink", ViewTask.class, new PageParameters("id=" + activity.getTask().getTaskId()));
        contentLink.setOutputMarkupId(true);

        activityContent = new Label("activityContent", activity.getContent());
        activityContent.setOutputMarkupId(true);
        contentLink.add(activityContent);

        String periodValue = "";
        PeriodFormatter periodFormat;
        Period period = new Period(activity.getTimestamp(), new Instant(), PeriodType.standard());
        PeriodFormatterBuilder periodFormatBuilder = new PeriodFormatterBuilder();
        if (period.toStandardSeconds().isGreaterThan(Seconds.seconds(60))) {
            if (period.toStandardMinutes().isGreaterThan(Minutes.minutes(60))) {
                if (period.toStandardHours().isGreaterThan(Hours.hours(24))) {
                    if (period.toStandardDays().isGreaterThan(Days.SIX)) {
                        periodFormatBuilder.appendWeeks().appendSuffix(" week", " weeks").appendSuffix(" ago");
                        periodFormat = periodFormatBuilder.toFormatter();
                        periodValue = periodFormat.print(period.toStandardWeeks());
                    } else {
                        periodFormatBuilder.appendDays().appendSuffix(" day", " days").appendSuffix(" ago");
                        periodFormat = periodFormatBuilder.toFormatter();
                        periodValue = periodFormat.print(period.toStandardDays());
                    }
                } else {
                    periodFormatBuilder.appendHours().appendSuffix(" hour", " hours").appendSuffix(" ago");
                    periodFormat = periodFormatBuilder.toFormatter();
                    periodValue = periodFormat.print(period.toStandardHours());
                }
            } else {
                periodFormatBuilder.appendMinutes().appendSuffix(" minute", " minutes").appendSuffix(" ago");
                periodFormat = periodFormatBuilder.toFormatter();
                periodValue = periodFormat.print(period.toStandardMinutes());
            }
        } else {
            periodFormatBuilder.appendSeconds().appendSuffix(" second", " seconds").appendSuffix(" ago");
            periodFormat = periodFormatBuilder.toFormatter();
            periodValue = periodFormat.print(period.toStandardSeconds());
        }

        timestampLabel = new Label("timestamp", periodValue) {

            private static final long serialVersionUID = 1L;

            @Override
            protected void onComponentTag(ComponentTag tag) {
                tag.setName("abbr");
                tag.getAttributes().put("title", DateTimeFormat.forPattern("EEEEE, d MMMMM yyyy 'at' HH:mm").print(activity.getTimestamp()));
            };
        };
        timestampLabel.setOutputMarkupId(true);

        contentLink.add(timestampLabel);
        add(contentLink);
    }

}
